package com.wyl.mobileplayer.ui.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;

import com.wyl.mobileplayer.R;
import com.wyl.mobileplayer.utils.LogUtils;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 时间：2018/6/23 0023 17:37
 * 描述：启动页面
 * 修改人：
 * 修改时间：
 * 修改备注：
 * <p>
 * 内容:
 * 1. 延时2秒进入主页面 RxJava
 * 2. 点击屏幕立即进入  onTouchEvent
 *
 * @author WangYoule
 * @qq 270628297
 */
public class SplashActivity extends AppCompatActivity {
    private Disposable disposable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        delayStartActivity(2, TimeUnit.SECONDS);
    }

    private void delayStartActivity(long delay, TimeUnit unit) {
        Observable.timer(delay, unit).subscribe(new Observer<Long>() {
            @Override
            public void onSubscribe(Disposable d) {
                disposable = d;
            }

            @Override
            public void onNext(Long aLong) {
                LogUtils.d("onNext: aLong = " + aLong);
                startMainActivity();
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void startMainActivity() {
        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_UP) {
            // 取消延时任务
            LogUtils.d("onTouchEvent: disposable.isDisposed() = " + disposable.isDisposed());
            disposable.dispose();
            startMainActivity();
        }
        return super.onTouchEvent(event);
    }
}


